package com.jh.notice.business.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.jh.notice.business.SmsSendBusiness;
import com.jh.notice.config.PropertiesConfig;
import com.jh.notice.pojo.SMSRecord;
import com.jh.notice.pojo.SMSRoute;
import com.jh.notice.repository.SmsRepository;
import com.jh.notice.repository.SmsRouteRepository;
import com.jh.notice.util.NoticeConstants;

import cn.jh.common.utils.DateUtil;


@Service
public class SmsSendBusinessImpl implements SmsSendBusiness{

	@Autowired
    private PropertiesConfig propertiesConfig;
	
	@Autowired
	private SmsRepository smsRepository;
	
	@Autowired
    private SmsRouteRepository smsRouteRepository;
	
	/**分页获取用户的记录*/
	@Override
	public List<SMSRecord> findSmsRecord(Pageable pageable, String phone, Date startTime,  Date endTime) {
		
		
		Page<SMSRecord>  result = null;
		if(phone !=null && !phone.equalsIgnoreCase("")){
			
			if(startTime !=  null){
				
				if(endTime != null){
					
					result = smsRepository.findSmsRecordByPhoneStartEndTime(phone, startTime, endTime, pageable);
					
				}else{
					
					result = smsRepository.findSmsRecordByPhoneStartTime(phone, startTime, pageable);
					
				}
				
			}else{
			
				result = smsRepository.findSmsRecord(phone,  pageable);
				
			}
			
		}else{
			
			if(startTime !=  null){
				
				if(endTime != null){
				
					result = smsRepository.findSmsRecordByStartEndTime(startTime, endTime, pageable);
					
				}else{
					
					result=  smsRepository.findSmsRecordByStartTime(startTime, pageable);	
					
				}
				
			}else{
				
				result = smsRepository.findAll(pageable);
				
			}
		}
		
		return result.getContent();
	
	}

	
	/**发送消息*/
	@Override
	public void sendSmsMessage(String phone,String tpl_id, Map<String, String> params,String ipAddress) {
		
		SMSRoute smsRoute = smsRouteRepository.getCurActiveSmsChannel();
	
		SMSRecord  record = new SMSRecord();
		String smsChannel = smsRoute.getCurSMSChannel();
		String sendSms = null;
		if(smsChannel.equalsIgnoreCase(NoticeConstants.SMS_CHANNEL_1)){
			
			sendSms = JuHeAPISmsService.sendSms(phone,tpl_id,propertiesConfig.getSmsUrl(),propertiesConfig.getSmsKey(),params);
		}
		System.out.println(sendSms);
		record.setContent(params.get("code"));
		record.setPhone(phone);
		record.setVeriCode(params.get("code"));
		record.setIpAddress(ipAddress);
		record.setCreateTime(new Date());
		/**存短信进入数据库**/
		smsRepository.save(record);
	
	}


	@Override
	public String querySmscodeByPhone(String phone) {
		SMSRecord lastestSmsRecord = smsRepository.findLastestSmsRecord(phone);
		if(lastestSmsRecord != null ) {
			return lastestSmsRecord.getVeriCode();
		}else {
			return null; 
		}
	}


	@Override
	public int findCountByIpAddress(String ipAddress) {
		return smsRepository.findCountByIpAddress(ipAddress,new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
	}


	@Override
	public int findCountByPhoneAndDate(String phone) {
		return smsRepository.findCountByPhoneAndDate(phone,DateUtil.getDateStringConvert(new String(), new Date(), "yyyy-MM-dd"));
	}


}
